﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace YNCNumberSys
{
    /// <summary>
    /// Access数据库操作类，封装了执行SQL语句的过程 
    /// </summary>
    public class AccessDB
    {
        //private static readonly string con = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=";
        //private static System.Data.SqlClient.SqlConnection dbCon;
        //private static System.Data.SqlClient.SqlDataAdapter dbAdapter;
        private static System.Data.OleDb.OleDbConnection dbCon;
        private static System.Data.OleDb.OleDbDataAdapter dbAdapter;
        private static DataTable dt;

        #region 打开数据库连接
        public static void ConOpen(string strDBPath)
        {
            if (dbCon == null)
            {
                //dbCon = new System.Data.SqlClient.SqlConnection(strDBPath);
                dbCon = new System.Data.OleDb.OleDbConnection(strDBPath);
            }
            if (dbCon.State != System.Data.ConnectionState.Open)
            {
                try
                {
                    //dbCon = new System.Data.SqlClient.SqlConnection(strDBPath);
                   //dbCon = new System.Data.OleDb.OleDbConnection(strDBPath);
                    dbCon.Open();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
        }
        #endregion 打开数据库连接

        #region 关闭数据库连接
        public static void ConClose()
        {
            if (dbCon != null)
            {
                dbCon.Close();
                //dbCon.Dispose();
            }
        }
        #endregion 关闭数据库连接

        #region 执行纯SQL语句，返回DataTable
        public static DataTable ExectueSqlOnce(string sql, string strDBPath)
        {
            DataTable table=new DataTable();
            using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(strDBPath))
            {
                connection.Open();
                using (System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(sql, connection))
                {
                    adapter.Fill(table);
                }
            }
            sql = " ";
            return table;
        }

        public static DataTable ExectueSql(string sql, string strDBPath)
        {
            ConOpen(strDBPath);
            //dbAdapter = new System.Data.SqlClient.SqlDataAdapter(sql, dbCon);
            dbAdapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbCon);
            dt = new DataTable();
            dbAdapter.Fill(dt);
            ConClose();
            sql = " ";
            return dt;
        }

        #endregion 执行纯SQL语句，返回DataTable

        #region 判断系统版本

        //C#判断操作系统是否为Windows98
        public static bool IsWindows98
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32Windows) && (Environment.OSVersion.Version.Minor == 10) && (Environment.OSVersion.Version.Revision.ToString() != "2222A");
            }
        }
        //C#判断操作系统是否为Windows98第二版
        public static bool IsWindows98Second
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32Windows) && (Environment.OSVersion.Version.Minor == 10) && (Environment.OSVersion.Version.Revision.ToString() == "2222A");
            }
        }
        //C#判断操作系统是否为Windows2000
        public static bool IsWindows2000
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor == 0);
            }
        }
        //C#判断操作系统是否为WindowsXP
        public static bool IsWindowsXP
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor == 1);
            }
        }
        //C#判断操作系统是否为Windows2003
        public static bool IsWindows2003
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor == 2);
            }
        }
        //C#判断操作系统是否为WindowsVista
        public static bool IsWindowsVista
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major == 6) && (Environment.OSVersion.Version.Minor == 0);
            }
        }
        //C#判断操作系统是否为Windows7
        public static bool IsWindows7
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major == 6) && (Environment.OSVersion.Version.Minor == 1);
            }
        }
        //C#判断操作系统是否为Unix
        public static bool IsUnix
        {
            get
            {
                return Environment.OSVersion.Platform == PlatformID.Unix;
            }
        }

        #endregion
    }
}
